jQuery(function() {
	var cookie = $.AMUI.utils.cookie;
	var sessionId = (cookie.get("sessionId") == null) ? "" : cookie.get("sessionId");
	var factoryId = GetQueryString("factoryId");
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		/*if(null == sessionId || "undefined" == sessionId || "" == sessionId) {
			window.location.href = "login.html";
		} else {*/
			$.ajax(locat + "/appClient/client_list", {
				type: "POST",
				xhrFields: {
					withCredentials: true,
					useDefaultXhrHeader: false
				},
				data: {
					'factoryid': factoryId
				},
				crossDomain: true,
				success: function(data, status, xhr) {
					if("error" == data["msg"])
						window.location.href = "login.html";
					else {
						var finallist = ""
						if("" == data["jsonListClient"]) {
							finallist = "<img  src='img/ts.png'>";
							finallist1 = " <button type='button' class='am-btn am-btn-block am-btn-success am-btn-kx' >" + data["factoryName"] + "</button>";
						} else {
							finallist1 = " <button type='button' class='am-btn am-btn-block am-btn-success am-btn-kx' >" + data["factoryName"] + "</button>" +
								"<div class='am-input-group am-input-group-sm'> <input type='text' class='am-form-field' id='clientName' " 
								+ "value=''  placeholder='客户名称'> <span class='am-input-group-btn'> "
								+ "<button class='am-btn am-btn-default' onclick='serch()' type='button'>搜索</button></span></div>";

							for(i = data["jsonListClient"].length - 1; i >= 0; i--) {
								finallist += "<div class='am-panel am-panel-default' >" +
									"<div class='am-panel-hd am-g'>" +
									"<div class='am-u-sm-9'>" +
									"<h4 onclick='findorderlist(\"" + data['jsonListClient'][i]['c_id'] + "\",\"" + data['jsonListClient'][i]['clientnumber'] + "\",\"" + data['jsonListClient'][i]['clientname'] + "\")'    class=\"am-panel-title\" data-am-collapse=\"{parent: '#accordion', target: '#div_" + data['jsonListClient'][i]['clientnumber'] + "'}\">" +
									data["jsonListClient"][i]["clientname"] + " </h4> </div>" +
									"<span class='am-fr'>查看订单<i class='am-icon-angle-right am-icon-sm am-icon-left'></i></span> </div>" +
									"<div id='div_" + data['jsonListClient'][i]['clientnumber'] + "' class='am-panel-collapse am-collapse'>" +
									"<div class='am-panel-bd' ></div></div></div><br> ";
							}
						}
						$("#factoryName").append(finallist1);
						$("#accordion").append(finallist);
					}

				},
				error: function(data) {
					swal("", "服务异常", "error");
					window.location.href = login.html;
				}
			});
		//}

	});

});

//搜索客户名称

function serch() {

	var cookie = $.AMUI.utils.cookie;
	var sessionId = (cookie.get("sessionId") == null) ? "" : cookie.get("sessionId");
	var factoryId = GetQueryString("factoryId");
	$.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
		var clientName = $("#clientName").val();
		if(null == sessionId || "" == sessionId || "" == sessionId) {
			window.location.href = "login.html";
		} else {
			$.ajax(locat + "/appClient/client_list", {
				type: "POST",
				xhrFields: {
					withCredentials: true,
					useDefaultXhrHeader: false
				},
				data: {
					'factoryid': factoryId,
					'clientName' : clientName
				},
				crossDomain: true,
				success: function(data, status, xhr) {
					if("error" == data["msg"])
						window.location.href = "login.html";
					else {
						$("#factoryName").empty();
						$("#accordion").empty();
						var finallist = ""
						if("" == data["jsonListClient"]) {
							finallist = "<img  src='img/ts.png'>";
						} else {
							finallist1 = " <button type='button' class='am-btn am-btn-block am-btn-success am-btn-kx' onclick='addorder();'>" + data["factoryName"] + "</button>" +
								"<div class='am-input-group am-input-group-sm'> <input type='text' class='am-form-field' id='clientName' value='"+data["clientName"]+"'  placeholder='客户名称'> <span class='am-input-group-btn'><button class='am-btn am-btn-default' onclick='serch()' type='button'>搜索</button></span></div>";

							for(i = data["jsonListClient"].length - 1; i >= 0; i--) {
								finallist += "<div class='am-panel am-panel-default' >" +
									"<div class='am-panel-hd am-g'>" +
									"<div class='am-u-sm-9'>" +
									"<h4 onclick='findorderlist(\"" + data['jsonListClient'][i]['c_id'] + "\",\"" + data['jsonListClient'][i]['clientnumber'] + "\",\"" + data['jsonListClient'][i]['clientname'] + "\")'    class=\"am-panel-title\" data-am-collapse=\"{parent: '#accordion', target: '#div_" + data['jsonListClient'][i]['clientnumber'] + "'}\">" +
									data["jsonListClient"][i]["clientname"] + " </h4> </div>" +
									"<span class='am-fr'>查看订单<i class='am-icon-angle-right am-icon-sm am-icon-left'></i></span> </div>" +
									"<div id='div_" + data['jsonListClient'][i]['clientnumber'] + "' class='am-panel-collapse am-collapse'>" +
									"<div class='am-panel-bd' ></div></div></div><br> ";
							}
						}
						$("#factoryName").append(finallist1);
						$("#accordion").append(finallist);
					}

				},
				error: function(data) {
					swal("", "服务异常", "error");
					window.location.href = login.html;
				}
			});
		}

	});

}

//加载 大票号		
function findorderlist(c_id, clientnumber, clientname) {
	var cookie = $.AMUI.utils.cookie;
	var sessionId = (cookie.get("sessionId") == null) ? "" : cookie.get("sessionId");
	var biaoshi = document.getElementsByName("khf_biaoshi");
	if($("#flag").val() == "0" || ($("#flag").val().indexOf('div_' + clientnumber) < 0) || biaoshi.length == 0) {
		var id = "body-loading";
		$(window).loading({
			action: "open",
			id: id,
			font: 40
		});
		$.getJSON("json/locat.json", function(data) {
			var locat = data["url"];
			var factoryid = data["factoryid"];
			$.ajax(locat + "/clientUser/ordr_list?clientnumber=" + clientnumber + "&factoryid=" + factoryid + "&sessionId=" + sessionId, {
				type: "GET",
				xhrFields: {
					withCredentials: true,
					useDefaultXhrHeader: false
				},
				crossDomain: true,
				success: function(data, status, xhr) {
					var time = setTimeout(function() {
						//关闭
						$(window).loading({
							action: "close",
							id: id
						});
					}, 100);
					if("error" == data["msg"])
						window.location.href = locat + "/clientUser/tologin";
					else {
						var finallist = " <ul class=\"am-list\">   	";
						if(data["json_card_orders"].length == 0) {

							sweetAlert("订单获取失败", "请先去工厂下单", "error");
							finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
								"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "' >" +
								"      <h3 class=\"am-list-item-hd\">" +
								"      	<a href=\"#\"  disabled='true'>订单编号：无</a>" +
								"      </h3>" +
								"      <div class=\"am-list-item-texta\">" +
								"      	<span class=\"am-time\">开票时间：无</span><br />" +
								"      	<span class=\"am-spmc\">商品名称：无</span><br />" +
								"      	<span class=\"am-sysl\">剩余数量：0吨</span><br />    " +
								"      	</div>" +
								"  </div>" +
								" </li>";
						} else {

							for(i = data["json_card_orders"].length - 1; i >= 0; i--) {
								if(data["json_card_orders"][i]["maxnumber"] == 0) {
									if(data["json_card_orders"][i]["salearea"] == "") {
										var stockname = data["json_card_orders"][i]["stockname"];
										var stockname1 = stockname.substring(0, stockname.length - 2);
										var stockname2 = stockname.substring(stockname.length - 2, stockname.length);
										finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
											"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "'  >" +
											"      <h3 class=\"am-list-item-hd\">" +
											"      	<a href=\"#\" disabled='true' class=\"\">订单编号：" + data["json_card_orders"][i]["billnumber"] + "</a>" +
											"      </h3>" +
											"      <div class=\"am-list-item-texta\">" +
											"      	<span class=\"am-time\">开票时间：" + data["json_card_orders"][i]["setdate"] + "</span><br />" +
											"      	<span class=\"am-spmc\">商品名称：" + stockname1 + "</span><span>" + stockname2 + "</span><br />" +
											"      	<span class=\"am-sysl\">剩余数量：" + data["json_card_orders"][i]["maxnumber"] + "吨</span><br />    " +
											"      	</div>" +
											"      	<button type=\"button\"  class=\"am-btn am-th am-radius am-fr am-btn-bom am-btn-block am-btn-warning\" >剩余数量为0，不能提货</button>	" +
											"  </div>" +
											" </li>";
									} else {
										var stockname = data["json_card_orders"][i]["stockname"];
										var stockname1 = stockname.substring(0, stockname.length - 2);
										var stockname2 = stockname.substring(stockname.length - 2, stockname.length);
										finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
											"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "'  >" +
											"      <h3 class=\"am-list-item-hd\">" +
											"      	<a href=\"#\" disabled='true' class=\"\">订单编号：" + data["json_card_orders"][i]["billnumber"] + "</a>" +
											"      </h3>" +
											"      <div class=\"am-list-item-texta\">" +
											"      	<span class=\"am-time\">工程工地：" + data["json_card_orders"][i]["salearea"] + "</span><br />" +
											"      	<span class=\"am-time\">开票时间：" + data["json_card_orders"][i]["setdate"] + "</span><br />" +
											"      	<span class=\"am-spmc\">商品名称：" + stockname1 + "</span><span>" + stockname2 + "</span><br />" +
											"      	<span class=\"am-sysl\">剩余数量：" + data["json_card_orders"][i]["maxnumber"] + "吨</span><br />    " +
											"      	</div>" +
											"      	<button type=\"button\"  class=\"am-btn am-th am-radius am-fr am-btn-bom am-btn-block am-btn-warning\" >剩余数量为0，不能提货</button>	" +
											"  </div>" +
											" </li>";
									}

								} else {
									if(data["json_card_orders"][i]["salearea"] == "") {
										var stockname = data["json_card_orders"][i]["stockname"];
										var stockname1 = stockname.substring(0, stockname.length - 2);
										var stockname2 = stockname.substring(stockname.length - 2, stockname.length);
										if(data["json_card_orders"][i]["remark"] == "") {
											finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
												"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "'  >" +
												"      <h3 class=\"am-list-item-hd\">" +
												"      	<a href=\"#\" disabled='true' class=\"\">订单编号：" + data["json_card_orders"][i]["billnumber"] + "</a>" +
												"      </h3>" +
												"      <div class=\"am-list-item-texta\">" +
												"      	<span class=\"am-time\">开票时间：" + data["json_card_orders"][i]["setdate"] + "</span><br />" +
												"      	<span class=\"am-spmc\">商品名称：" + stockname1 + "</span><span class='am-stockname'>" + stockname2 + "</span><br />" +
												"      	<span class=\"am-sysl\">剩余数量：" + data["json_card_orders"][i]["maxnumber"] + "吨</span><br />    " +
												"      	</div>" +
												"      	<button type=\"button\" onclick='place_order(\"" + data['json_card_orders'][i]['stockno'] + "\",\"" + data['json_card_orders'][i]['stockname'] + "\",\"" + data['json_card_orders'][i]['billnumber'] + "\",\"" + data['json_card_orders'][i]['maxnumber'] + "\",\"" + clientname + "\" ,\"" + clientnumber + "\" ,\"" + c_id + "\" ,\"" + factoryid + "\",\"" + data["json_card_orders"][i]["salearea"] + "\"      )' class=\"am-btn am-th am-radius am-fr am-btn-bom am-btn-block am-btn-secondary\">我要提货</button>	" +

												"  </div>" +
												" </li>";
										} else {
											finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
												"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "'  >" +
												"      <h3 class=\"am-list-item-hd\">" +
												"      	<a href=\"#\" disabled='true' class=\"\">订单编号：" + data["json_card_orders"][i]["billnumber"] + "</a>" +
												"      </h3>" +
												"      <div class=\"am-list-item-texta\">" +
												"      	<span class=\"am-time\">开票时间：" + data["json_card_orders"][i]["setdate"] + "</span><br />" +
												"      	<span class=\"am-spmc\">商品名称：" + stockname1 + "</span><span class='am-stockname'>" + stockname2 + "</span><br />" +
												"      	<span class=\"am-sysl\">剩余数量：" + data["json_card_orders"][i]["maxnumber"] + "吨</span><br />    " +
												"      	<span class=\"am-sysl\">标段备注：" + data["json_card_orders"][i]["remark"] + "</span><br />    " +
												"      	</div>" +
												"      	<button type=\"button\" onclick='place_order2(\"" + data['json_card_orders'][i]['stockno'] + "\",\"" + data['json_card_orders'][i]['stockname'] + "\",\"" + data['json_card_orders'][i]['billnumber'] + "\",\"" + data['json_card_orders'][i]['maxnumber'] + "\",\"" + clientname + "\" ,\"" + clientnumber + "\" ,\"" + c_id + "\" ,\"" + data["json_card_orders"][i]["remark"] + "\"  ,\"" + factoryid + "\"     )' class=\"am-btn am-th am-radius am-fr am-btn-bom am-btn-block am-btn-secondary\">我要提货</button>	" +

												"  </div>" +
												" </li>";
										}

									} else {
										var stockname = data["json_card_orders"][i]["stockname"];
										var stockname1 = stockname.substring(0, stockname.length - 2);
										var stockname2 = stockname.substring(stockname.length - 2, stockname.length);
										finallist += "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">  " +
											"  <div class=\" am-u-sm-12 am-list-main\" name='khf_biaoshi" + data["clientnumber"] + "'  >" +
											"      <h3 class=\"am-list-item-hd\">" +
											"      	<a href=\"#\" disabled='true' class=\"\">订单编号：" + data["json_card_orders"][i]["billnumber"] + "</a>" +
											"      </h3>" +
											"      <div class=\"am-list-item-texta\">" +
											"      	<span class=\"am-time\">工程工地 ：" + data["json_card_orders"][i]["salearea"] + "</span><br />" +
											"      	<span class=\"am-time\">开票时间：" + data["json_card_orders"][i]["setdate"] + "</span><br />" +
											"      	<span class=\"am-spmc\">商品名称：" + stockname1 + "</span><span class='am-stockname'>" + stockname2 + "</span><br />" +
											"      	<span class=\"am-sysl\">剩余数量：" + data["json_card_orders"][i]["maxnumber"] + "吨</span><br />    " +
											"      	</div>" +
											"      	<button type=\"button\" onclick='place_order(\"" + data['json_card_orders'][i]['stockno'] + "\",\"" + data['json_card_orders'][i]['stockname'] + "\",\"" + data['json_card_orders'][i]['billnumber'] + "\",\"" + data['json_card_orders'][i]['maxnumber'] + "\",\"" + clientname + "\" ,\"" + clientnumber + "\" ,\"" + c_id + "\",\"" + factoryid + "\" ,\"" + data["json_card_orders"][i]["salearea"] + "\"      )' class=\"am-btn am-th am-radius am-fr am-btn-bom am-btn-block am-btn-secondary\">我要提货</button>	" +

											"  </div>" +
											" </li>";
									}
								}

							}
						}
						finallist += "</ul>";
						$("#flag").val('div_' + clientnumber + "," + $("#flag").val()); //flag 赋值;
						$('#div_' + clientnumber).empty(); //移除节点内的子元素
						$('#div_' + clientnumber).append(finallist);
					}

				},
				error: function(data) {
					var time = setTimeout(function() {
						//关闭
						$(window).loading({
							action: "close",
							id: id
						});
					}, 100);
					swal("", "服务异常", "error");
					window.location.href = login.html;
				}
			});
		});
	} else {
		//什么都不做；
		//alert($("name='biaoshi'")).val();
		//var biaoshi=document.getElementsByName("khf_biaoshi");
	}

}

//进入下单页面
function place_order(stockno, stockname, billnumber, maxnumber, clientname, clientnumber, c_id, factoryid, section) {
	var factoryId = GetQueryString("factoryId");
	window.location.href = "xthd.html?factoryId=" + factoryId+"&stockno="+stockno+"&section="+section+"&stockname="+stockname
	+"&billnumber="+billnumber+"&maxnumber="+maxnumber+"&clientname="+clientname+"&clientnumber="+clientnumber+"&c_id="+c_id;
}

//新增大票号
function addorder() {
	sweetAlert("网上下单业务未开启", "", "error");
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}